

function filteGoodsId(state,action){
    var ShopCartInfo = state.ShopCartInfo;
    let payload = action.payload;
    if(ShopCartInfo.length == 0){
        ShopCartInfo.push(payload);
    }else{

            // let doubleId = state.ShopCartInfo.filter((item,index)=>{
            //     return item.goodsId == action.payload.goodsId
            // })
            // console.log(doubleId)
            
            // if(doubleId.length == 0){
            //     state.ShopCartInfo.push(action.payload);
            //     localStorage.setItem("shopCart",JSON.stringify(state.ShopCartInfo))
            // }else if(doubleId[0].quantity >= action.payload.quantity){

            //     console.log(action.payload.quantity)
            //    

                {/* 此为简化版 */}
                //查询到的state内容，如果length为0，则代表可以插入数据。
                // 如果为1，则需要再判断quantity是否一致，不一致要替换内容。
                 //es6 方法，找到第一个匹配的index，之后再进行处理，更快捷
                var index = ShopCartInfo.findIndex(item =>{
                    return item.goodsId == payload.goodsId
                })
                //  console.log("index",index)
                if(index == -1){
                    ShopCartInfo.push(payload);
                }else if(index != -1 && ShopCartInfo[index].quantity < payload.quantity ){
                    ShopCartInfo.splice(index,1,payload)
                  
                }
                           
                //传统的map方法，中间循环判断id，找到index，之后改变值
                // var num =  state.ShopCartInfo.map((item,index)=>{
                //         if(item.goodsId == doubleId[0].goodsId ){      
                //             state.ShopCartInfo.splice(index,1,action.payload)
                //             localStorage.setItem("shopCart",JSON.stringify(state.ShopCartInfo))                  
                //             return 
                //         }
                // })
               
            // }
    }
    localStorage.setItem("shopCart",JSON.stringify(ShopCartInfo))  
}


function udpateGoods(state,action){
    

    let ShopCartInfo = state.ShopCartInfo;
    let payload = action.payload;
    var index = ShopCartInfo.findIndex(item =>{
        return item.goodsId == payload.item.goodsId
    })

    if(index!=-1){
        let quantity = ShopCartInfo[index].quantity

        if(payload.method=="add"){  
            if(quantity<99 && quantity>0){    
                ShopCartInfo[index].quantity++
            }
           
        }else if(payload.method=="substract") {
            if(quantity<100 && quantity>1){
                ShopCartInfo[index].quantity--
            }
            
        }
        localStorage.setItem("shopCart",JSON.stringify(ShopCartInfo))    
    }
             
        
            
    
}


function delGoods(state,action){
    

    let ShopCartInfo = state.ShopCartInfo;
    let payload = action.payload;

    var index = ShopCartInfo.findIndex(item =>{
        return item.goodsId == payload
     
    })


    if(index!=-1){
        ShopCartInfo.splice(index,1)  
        localStorage.setItem("shopCart",JSON.stringify(ShopCartInfo))  
        console.log("删除成功")
        
    }               
    
}
export  {filteGoodsId,udpateGoods,delGoods}